package com.teamwork.demo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.teamwork.demo.entity.Application;
import com.teamwork.demo.entity.Class;
import com.teamwork.demo.entity.Group;
import com.teamwork.demo.mapper.A_Mapper;
import com.teamwork.demo.mapper.C_Mapper;
import com.teamwork.demo.mapper.G_Mapper;
import com.teamwork.demo.mapper.StuMapper;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.teamwork.demo.entity.Stu;


import java.util.ArrayList;
import java.util.List;

@Service
public class StuService  implements com.teamwork.demo.service.StuService {
    @Autowired
    public StuMapper stuMapper;
    @Autowired
    private A_Mapper a_Mapper;
    @Autowired
    private G_Mapper g_Mapper;
    @Autowired
    public C_Mapper c_Mapper;

    @Override
    public Page<Stu> findAll(Stu stu , Integer page,Integer limit) {
        Specification<Stu> spe = new Specification<Stu>() {
            public Predicate toPredicate(Root<Stu> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<jakarta.persistence.criteria.Predicate> list = new ArrayList<>();
                if (ObjectUtil.isNotEmpty(stu.getSid())) {
                  list.add(cb.and(cb.equal(root.get("sid"), stu.getSid())));
                }

                if (ObjectUtil.isNotEmpty(stu.getSname())) {
                  list.add(cb.and(cb.equal(root.get("sname"), stu.getSname())));
                }

                if (ObjectUtil.isNotEmpty(stu.getSmajor())) {
                  list.add(cb.and(cb.equal(root.get("smajor"), stu.getSmajor())));
                }

                if (ObjectUtil.isNotEmpty(stu.getSgrade())) {
                  list.add(cb.and(cb.equal(root.get("sgrade"), stu.getSgrade())));
                }

                if (ObjectUtil.isNotEmpty(stu.getSgroup())) {
                  list.add(cb.and(cb.equal(root.get("group"), stu.getSgroup())));
                }
                jakarta.persistence.criteria.Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }

        };
        return stuMapper.findAll(spe,PageRequest.of(page,limit));
    };

    @Override
    public Stu save (Stu stu) {
        return stuMapper.save(stu);
    }

    @Override
    public Stu update(Stu stu){
        Stu oldSut = stuMapper.findById(stu.getSid()).orElse(null);
        if (oldSut == null) {
            return null;
        }
        stu.setSgrade(oldSut.getSgrade());
        stu.setSgroup(oldSut.getSgroup());
        return stuMapper.save(stu);
    }
    @Override
    public Page<Application> findAll(Application application, Integer page, Integer limit) {
        Specification<Application> spe = new Specification<Application>() {
            public Predicate toPredicate(Root<Application> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<jakarta.persistence.criteria.Predicate> list = new ArrayList<>();
                if (ObjectUtil.isNotEmpty(application.getId())) {
                    list.add(cb.and(cb.equal(root.get("sid"), application.getId())));
                }
                if (ObjectUtil.isNotEmpty(application.getSname())) {
                    list.add(cb.and(cb.equal(root.get("sname"), application.getSname())));
                }

                if (ObjectUtil.isNotEmpty(application.getSmajor())) {
                    list.add(cb.and(cb.equal(root.get("smajor"), application.getSmajor())));
                }
                if (ObjectUtil.isNotEmpty(application.getSex())) {
                    list.add(cb.and(cb.equal(root.get("sex"), application.getSex())));
                }
                if (ObjectUtil.isNotEmpty(application.getScondition())) {
                    list.add(cb.and(cb.equal(root.get("scondition"), application.getScondition())));
                }
                jakarta.persistence.criteria.Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }

        };

        return a_Mapper.findAll(spe,PageRequest.of(page,limit));
    }

    @Override
    public Application save(Application application) {
        return a_Mapper.save(application);
    }

    @Override
    public Application update(Application application){
        Application oldSut =a_Mapper.findById(application.getId()).orElse(null);
        application.setSname(oldSut.getSname());
        application.setSmajor(oldSut.getSmajor());
        application.setSex(oldSut.getSex());
        return a_Mapper.save(application);

    }

    @Override
    public Page<Group> findAll(Group group, int page, int limit ) {
        Specification<Group> spe = new Specification<Group>() {
            public Predicate toPredicate(Root<Group> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<jakarta.persistence.criteria.Predicate> list = new ArrayList<>();
                if (ObjectUtil.isNotEmpty(group.getId())) {
                    list.add(cb.and(cb.equal(root.get("gid"), group.getId())));
                }
                if (ObjectUtil.isNotEmpty(group.getGname())) {
                    list.add(cb.and(cb.equal(root.get("gname"), group.getGname())));
                }

                jakarta.persistence.criteria.Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        };
        return g_Mapper.findAll(spe,PageRequest.of(page,limit));
    }
    @Override
    public Page<Class> findAll(Class sclass, int page, int limit ) {
        Specification<Class> spe = new Specification<Class>() {
            public Predicate toPredicate(Root<Class> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<jakarta.persistence.criteria.Predicate> list = new ArrayList<>();
                if (ObjectUtil.isNotEmpty(sclass.getId())) {
                    list.add(cb.and(cb.equal(root.get("cid"), sclass.getId())));
                }
                if (ObjectUtil.isNotEmpty(sclass.getCname())) {
                    list.add(cb.and(cb.equal(root.get("cname"), sclass.getCname())));
                }
                if (ObjectUtil.isNotEmpty(sclass.getCteacher())) {
                    list.add(cb.and(cb.equal(root.get("cteacher"), sclass.getCteacher())));
                }

                jakarta.persistence.criteria.Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        };
        return c_Mapper.findAll(spe,PageRequest.of(page,limit));
    }
}



